<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/6/15
 * Time: 21:36
 */

namespace App\Services;


use App\Model\AppModel;

class AppService
{
    /**del app
     * @param $param
     * @param $adminInfo
     * @return int
     */
    public function delApp($param,$adminInfo){
        try{
            if(AppModel::create()->update(['status'=>AppModel::APP_STATUS_DISABLE],['id'=>$param['id']])){
                return ReponseCodeService::CODE_OK;
            }
            return ReponseCodeService::CODE_FAILE;
        }catch(\Throwable $t){
            return ReponseCodeService:: CODE_SERVICE_UNAVAILABLE;
        }
    }

    /**
     * @param $page
     * @param $limit
     * @param int $adminId
     * @return array|int
     */
    public function list($page,$limit,$adminId=0){
        try{
            $model = AppModel::create()->limit($limit * ($page - 1), $limit)->withTotalCount();
            // 列表数据
            $list = $model->all(null);
            $result = $model->lastQueryResult();
            // 总条数
            $total = $result->getTotalCount();
            return ['list'=>$list,'total'=>$total];
        }catch(\Throwable $t){
            return ReponseCodeService:: CODE_SERVICE_UNAVAILABLE;
        }
    }

    /**update app
     * @param $param
     * @param $adminInfo
     */
    public function updateApp($param,$adminInfo){
        try{
            $updateData = array();
          if(isset($param['appname'])&&!empty($param['appname'])){
            $updateData['appname'] = $param['appname'];
          }
            if(isset($param['status'])&&in_array($param['status'],[0,1,2])){
                $updateData['status'] = $param['status'];
            }

            if(sizeof($updateData)<=0){
                return ReponseCodeService:: CODE_EMPTY;
            }

            if(AppModel::create()->update($updateData,['id'=>$param['id']])){
                return AppModel::create()->get(['id'=>$param['id']]);
            }
            return ReponseCodeService:: CODE_SERVICE_UNAVAILABLE;
        }catch(\Throwable $t){
            return ReponseCodeService:: CODE_SERVICE_UNAVAILABLE;
        }
    }

    /**create app
     * @param $param
     */
    public function createApp($param,$adminInfo){
        try{
          $appData['appname'] = $param['appname'];
          $appData['admin_id'] = $adminInfo['id'];
          $appData['access_key'] = $this->createAccessKey();
          $appData['secret_key'] = $this->createSecretKey();
          $appData['status']     = AppModel::APP_STATUS_ACTIVE;
          $appData['created_at'] = date("Y-m-d H:i:s");
          $appData['updated_at'] = date("Y-m-d H:i:s");
          $id = AppModel::create()->data($appData)->save();
          $appData['id'] =  $id;
          return   $appData;
        }catch(\Throwable $t){
            return ReponseCodeService:: CODE_SERVICE_UNAVAILABLE;
        }
    }


    /**
     * create an access_key
     */
    public function createAccessKey(){
        return HelpService::createNonce(2).date("YmdHis").HelpService::createNonce(4);
    }

    /**
     * create a secret_key
     */
    public function createSecretKey(){
        return HelpService::createNonce(12);
    }


    /**
     *通过ak获取app
     */
    function  findApp($accessKey){
         $app = AppModel::create()->get(['access_key'=>$accessKey]);
         return  $app;
    }
}